package com.inspector.app.di.persistence.plot

import androidx.room.*
import androidx.sqlite.db.SupportSQLiteQuery
import com.mapsforge.utils.MapUtils
import kotlinx.coroutines.flow.Flow


@Dao
interface MapItemDao {

    //[插入单条数据]
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertItem(items: MapItem): Long

    //[更新单条数据]
    @Update(onConflict = OnConflictStrategy.REPLACE)
    suspend fun updateItem(vararg items: MapItem): Int

    //[更新数据集合]
    @Update(onConflict = OnConflictStrategy.REPLACE)
    suspend fun updateItems(items: List<MapItem>)

//    //[根据mapID查询数据]
//    @Query("SELECT * FROM MapItem WHERE mapID = :mapID")
//    fun getItem(mapID: Long?): MapItem?

    //[根据飞行元素id查询数据]
    @Query("SELECT * FROM MapItem WHERE flyElementId = :flyElementId")
    suspend fun getItem(flyElementId: String?): MapItem?

    //[更新数据,存在则更新-此方法可刷新订阅adapter]
    suspend fun insertOrUpdate(item: MapItem): MapItem {
        if (item.flyElementId.isNullOrEmpty()) {
            item.flyElementId = MapUtils.UUIDGenerator()
        }
        val itemFromDB = getItem(item.flyElementId)
        if(itemFromDB==null){
            insertItem(item)
            return item
        }
        val newItem = item.copy()
        newItem.mapID = itemFromDB.mapID
        newItem.visible = itemFromDB.visible
        newItem.delete = itemFromDB.delete
        newItem.upload = itemFromDB.upload
        newItem.createTime = itemFromDB.createTime
        newItem.updateTime = item.createTime
        updateItem(newItem)
        return newItem
    }

    //[根据航线、航点、空域、禁飞区、障碍物等类型查询] https://stackoverflow.com/questions/47730820/hardcode-boolean-query-in-room-database
    /*@Query("SELECT * FROM mapitem WHERE layerType = :layerType AND `delete` = 0 ORDER BY createTime DESC")
    fun getItems(layerType: String): Flow<MutableList<MapItem>>*/

    @Query("SELECT * FROM mapitem WHERE jsonType = :jsonType AND layerType = :layerType AND `delete` = 0 ORDER BY createTime DESC")
    fun getItems(jsonType: String, layerType: String): Flow<MutableList<MapItem>>


    @Query("SELECT * FROM mapitem WHERE layerType = :layerType AND `delete` = 0 ORDER BY createTime DESC")
    fun getItemsByLayerType(layerType: String): Flow<MutableList<MapItem>>


    @Query("SELECT * FROM MapItem WHERE jsonType = :jsonType")
    suspend fun getItemsByJsonType(jsonType: String): MutableList<MapItem>


    //[删除数据 慎用,确保上传完服务器,更新完上传状态在删除]
    @Query("DELETE FROM mapitem WHERE mapID IN (:mapIds)")
    suspend fun deleteItemByIds(mapIds: MutableList<Long>)


    //[删除元素]
    @Query("DELETE FROM mapitem WHERE flyElementId = :flyElementId")
    suspend fun deleteItemByFlyElementId(flyElementId: String?)

    //[获取RowId]
    @RawQuery
    fun getPointDataBaseRowId(query: SupportSQLiteQuery): Int

    //[未删除状态的模糊搜索]
    @Query("SELECT * FROM MapItem WHERE jsonType = :jsonType AND layerType = :layerType AND `delete` = :isDelete AND name LIKE '%' || :name || '%'")
    fun searchItems(jsonType: String, layerType: String, name: String, isDelete: Boolean): Flow<MutableList<MapItem>>


    //[通过判断条件获取待上传的item]
    @Query("SELECT * FROM MapItem WHERE upload = :upload AND `delete` = :isDelete")
    suspend fun getUploadItems(upload: Boolean, isDelete: Boolean): MutableList<MapItem>


    //[通过判断条件获取需要删除的item]
    @Query("SELECT * FROM MapItem WHERE uploaded = :uploaded AND `delete` = :isDelete")
    suspend fun getNeedDeleteItems(uploaded: Boolean, isDelete: Boolean): MutableList<MapItem>



}